package org.itli.jnsocket.Service.ServiceImpl;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.itli.jnsocket.Mapper.SupplierMapper;
import org.itli.jnsocket.Pojo.Supplier;
import org.itli.jnsocket.Service.SupplierService;
import org.itli.jnsocket.Util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SupplierServiceImpl implements SupplierService {
    SupplierMapper mapper;
    Gson gson;

    @Autowired
    public void setMapper(SupplierMapper mapper) {
        this.mapper = mapper;
    }

    @Autowired
    public void setGson(Gson gson) {
        this.gson = gson;
    }

    @Override
    public String AllSuppliers(PageUtil page) {
        try{
            List<Supplier> suppliers = mapper.AllSuppliers(page.getSize(),page.getPage());
            String json = gson.toJson(suppliers);
            return json;
        } catch (Exception e) {
            log.info(e.getLocalizedMessage());
            return "error";
        }
    }

    @Override
    public String AddSupplier(Supplier supplier,String username) {
        try{
            int i = mapper.AddSupplier(supplier, username, username);
            if(i == 1){
                return "1";
            }else{
                return "0";
            }
        } catch (Exception e) {
            log.info(e.getLocalizedMessage());
            return "error";
        }
    }

    @Override
    public String AlterSipplier(Supplier supplier, String username) {
        try{
            mapper.UpdateSupplier(supplier, username);
            return "1";
        } catch (Exception e) {
            log.info(e.getLocalizedMessage());
            return "error";
        }
    }

    @Override
    public String DeleteSupplier(Supplier supplier) {
        try{
            mapper.DeleteSupplier(supplier.getId());
            return "1";
        } catch (Exception e) {
            log.info("错误信息"+e);
            return "error";
        }
    }
}
